<include file="public@header"/>

<body>
<div class="wrap js-check-wrap">
    <ul class="nav nav-tabs">
        <li class="active"><a href="{:url('Classes/student_leave')}">请假列表</a></li>
    </ul>
    <form class="well form-inline margin-top-20 row" method="post" action="{:url('Classes/student_leave')}">
        <label class="form-group col-lg-5 col-md-6" style="margin-bottom:15px">
            学校-班级:
            <select class="form-control" name="school_id" id="school_id" style="width:179px;">
                <option value='0'>全部</option>
                <notempty name="campus">
                    <volist name="campus" id="vo">
                        <if condition="isset($param.school_id) && $param.school_id == $vo.id">
                            <option value="{$vo.id}" selected>{$vo.name}</option>
                        <else/>
                            <option value="{$vo.id}">{$vo.name}</option>
                        </if>
                    </volist>
                </notempty>
            </select> -
            <select class="form-control" name="class_id" id="class_id" style="width:179px;">
                <option value='0'>全部</option>
                <notempty name="classes">
                    <volist name="classes" id="vo">
                        <if condition="isset($param.class_id) && $param.class_id == $vo.id">
                            <option value="{$vo.id}" selected>{$vo.name}</option>
                            <else/>
                            <option value="{$vo.id}">{$vo.name}</option>
                        </if>
                    </volist>
                </notempty>
            </select>
        </label>

        <label class="form-group col-lg-4 col-md-3" style="margin-bottom:15px">
            课程:
            <select class="form-control" name="course_id" id="course_id" >
                <option value='0'>全部</option>
                <notempty name="course">
                    <volist name="course" id="vo">
                        <if condition="isset($param.course_id) && $param.course_id == $vo.id">
                            <option value="{$vo.id}" selected>{$vo.course_name}</option>
                            <else/>
                            <option value="{$vo.id}">{$vo.course_name}</option>
                        </if>
                    </volist>
                </notempty>
            </select>
        </label>


        <label class="form-group col-lg-5 col-md-6">
            上课时间:&nbsp;&nbsp;
            <input type="text" class="form-control" name="start"
                   value="{$param.start|default=''}"
                   autocomplete="off"> -
            <input type="text" class="form-control" name="end"
                   value="{$param.end|default=''}"
                   autocomplete="off">
        </label>

        <label class="form-group col-lg-3 col-md-3" >
            补课方式:
            <select class="form-control" name="change_course">
                <option value='-1'>全部</option>
                <if condition="isset($param.change_course) && $param.change_course == 0">
                    <option value="0" selected>未申请</option>
                    <else/>
                    <option value="0">未申请</option>
                </if>
                <if condition="isset($param.change_course) && $param.change_course == 1">
                    <option value="1" selected>申请录音</option>
                    <else/>
                    <option value="1">申请录音</option>
                </if>
                <if condition="isset($param.change_course) && $param.change_course == 2">
                    <option value="2" selected>申请调课</option>
                    <else/>
                    <option value="2">申请调课</option>
                </if>
            </select>
        </label>

        <!--<label class="form-group col-lg-3 col-md-3">-->
            <!--请假状态:-->
            <!--<select class="form-control" name="status">-->
                <!--<option value='-1'>全部</option>-->
                <!--<if condition="isset($param.status) && $param.status == 0">-->
                    <!--<option value="0" selected>未审批</option>-->
                <!--<else/>-->
                    <!--<option value="0">未审批</option>-->
                <!--</if>-->
                <!--<if condition="isset($param.status) && $param.status == 1">-->
                    <!--<option value="1" selected>已批准</option>-->
                <!--<else/>-->
                    <!--<option value="1">已批准</option>-->
                <!--</if>-->
                <!--<if condition="isset($param.status) && $param.status == 2">-->
                    <!--<option value="2" selected>已拒绝</option>-->
                <!--<else/>-->
                    <!--<option value="2">已拒绝</option>-->
                <!--</if>-->
            <!--</select>-->
        <!--</label>-->

        <label class="form-group col-lg-4 col-md-3">
             <input type="submit" class="btn btn-primary" value="搜索"/>
            <a class="btn btn-danger" href="{:url('Classes/student_leave')}">清空</a>
        </label>
    </form>

    <form class="js-ajax-form" action="" method="post">
        <table class="table table-hover table-bordered table-list">
            <thead>
            <tr>
                <th>编号</th>
                <th>学生</th>
                <th>学校</th>
                <th>班级</th>
                <th>课程</th>
                <th>授课老师</th>
                <th>请假课时</th>
                <th>课时进度</th>
                <th>请假原因</th>
                <th>补课方式</th>
                <th>补课状态</th>
                <th>操作</th>
            </tr>
            </thead>
            <notempty name="leaves">
                <foreach name="leaves" item="vo">
                    <tr>
                        <td>{$vo.id}</td>
                        <td>{$vo.student_name}</td>
                        <td>{$vo.campus_name}</td>
                        <td>{$vo.class_name}</td>
                        <td>{$vo.course_name}</td>
                        <td>{$vo.teacher_name}</td>
                        <td>{$vo.date|date="Y-m-d",###} {$vo.start_time}-{$vo.end_time} {$vo.week}</td>
                        <td>{$vo.schedule}/{$vo.course_hour}</td>
                        <td>{$vo.reason}</td>
                        <td>
                            <if condition="$vo.change_course eq 1">申请录音
                            <elseif condition="$vo.change_course eq 2"/>申请调课
                            <else/>未申请
                            </if>
                        </td>
                        <td>
                            <if condition="$vo.status eq 0">
                                <span class="label label-warning">未处理</span>
                            <elseif condition="$vo.status eq 1"/>
                                <if condition="$vo.change_course eq 2">
                                    <span class="label label-success">调课申请成功</span>
                                <elseif condition="$vo.change_course eq 1"/>
                                    <span class="label label-success">录音申请成功</span>
                                <else/>
                                </if>
                            <elseif condition="$vo.status eq 2"/>
                                <span class="label label-danger">拒绝</span>
                            <else/>
                            </if>
                        </td>

                        <td>
                            <if condition="$vo.status eq 0 && $vo.change_course eq 0">
                                <a href="javascript:;" data-leave="{$vo.id}" data-student="{$vo.student_id}" data-school="{$vo.school_id}" data-class="{$vo.class_id}" class="adjust_schedule">调课</a> |
                                <a href="{:url('Classes/handle_leave',array('id'=>$vo.id, 'change_course'=>'y'))}" data-msg="确定要同意录音吗？" class="js-ajax-delete">同意录音</a>
                            </if>

                            <if condition="$vo.status eq 0 && $vo.change_course neq 0">
                                <if condition="$vo.change_course eq 1">
                                    <a href="{:url('Classes/handle_leave',array('id'=>$vo.id, 'change_course'=>'y'))}" data-msg="确定要同意录音吗？" class="js-ajax-delete">同意录音</a> |
                                <else/>
                                    <a href="javascript:;" data-leave="{$vo.id}" data-student="{$vo.student_id}" data-school="{$vo.school_id}" data-class="{$vo.class_id}" class="adjust_schedule">
                                        <if condition="isset($vo.adjust_id) && $vo.adjust_id gt 0">
                                            重新调课 <span style="text-decoration: none;color:#333;cursor: default;cursor:text;">|</span>
                                            <a href="javascript:;" data-leave="{$vo.id}" data-student="{$vo.student_id}" data-school="{$vo.school_id}" data-class="{$vo.class_id}" class="adjust_detail">详情</a>
                                            <else/>
                                            调课
                                        </if>
                                    </a> |
                                </if>

                                <a href="{:url('Classes/handle_leave',array('id'=>$vo.id, 'change_course'=>'n'))}" data-msg="确定要拒绝吗？" class="js-ajax-delete">拒绝</a>
                            </if>

                            <if condition="$vo.status eq 1 && $vo.change_course neq 0 && $vo.change_course neq 1">
                                <a href="javascript:;" data-leave="{$vo.id}" data-student="{$vo.student_id}" data-school="{$vo.school_id}" data-class="{$vo.class_id}" class="adjust_schedule">
                                    重新调课 <span style="text-decoration: none;color:#333;cursor: default;cursor:text;">|</span>
                                    <a href="javascript:;" data-leave="{$vo.id}" data-student="{$vo.student_id}" data-school="{$vo.school_id}" data-class="{$vo.class_id}" class="adjust_detail">详情</a>
                                </a>
                            </if>
                        </td>
                    </tr>
                </foreach>
            </notempty>
        </table>
        <div class="pagination">{$page|default=''}</div>
    </form>
</div>

<div id="adjust_course_schedule" style="display: none;">
    <form id="form_adjust" action="" class="row well" style="margin:0;">
        <input type="hidden" name="leave_id" id="leave_id" value="0">
        <input type="hidden" name="student_id" id="student_id" value="0">
        <input type="hidden" name="old_school_id" id="old_school_id" value="0">
        <input type="hidden" name="old_class_id" id="old_class_id" value="0">

        <label class="form-group form-inline col-xs-12" style="margin-bottom:15px">学校:
            <select class="form-control" name="adjust_school_id" id="adjust_school_id" style="width:240px;">
                <option value='0'>--- 请选择 ---</option>
                <notempty name="campus">
                    <volist name="campus" id="vo">
                        <option value="{$vo.id}">{$vo.name}</option>
                    </volist>
                </notempty>
            </select>
        </label>

        <label class="form-group form-inline col-xs-12" style="margin-bottom:15px">班级:
            <select class="form-control" name="adjust_class_id" id="adjust_class_id" style="width:240px;">
                <option value='0'>--- 请选择 ---</option>
            </select>
        </label>

        <label class="form-group form-inline col-xs-12" style="margin-bottom:15px">课时:
            <select class="form-control" name="adjust_course_time" id="adjust_course_time" style="width:240px;">
                <option value='0'>--- 请选择 ---</option>
            </select>
        </label>
    </form>
</div>

<div id="adjust_schedule_detail" style="display: none;">
    <form id="form_adjust_detail" action="" class="row well" style="margin:0;background-color: #fff;">
        <table id="table_adjust" class="table table-hover table-bordered table-list">

        </table>
    </form>
</div>

<script src="__STATIC__/js/admin.js"></script>
<script>
    $(document).ready(function () {
        //搜索栏根据学校选择班级
        $('#school_id').on('change',function () {
            var sid = $(this).val();
            if(sid == 0){
                $('#class_id').empty();
                var html = '<option value=\"0\">全部</option>';
                $('#class_id').html(html);
                return false;
            }
            $.ajax({
                url:"{:url('Classes/ajax_class')}",
                type:'post',
                dataType:'json',
                data:{"school_id":sid},
                success:function(jsonData){
                    $('#class_id').empty();
                    var html = '<option value=\"0\">全部</option>';

                    if(jsonData.code == 1){
                        for(var i in jsonData.data){
                            html += '<option value=\"'+jsonData.data[i].id+'\">'+jsonData.data[i].name+'</option>';
                        }
                    }
                    $('#class_id').html(html);
                },
                error:function(XMLHttpRequest, textStatus, errorThrown){
                    console.log('xhr='+XMLHttpRequest.readyState+' status='+textStatus+' error='+errorThrown);
                }
            });
        });

        $('input[name="start"]').datepicker();
        $('input[name="end"]').datepicker();

    });

    /**************调课****************/
    $(document).on('click','.adjust_schedule',function () {
        var leaveId = $(this).data('leave');
        var studentId = $(this).data('student');
        var schoolId = $(this).data('school');
        var classId = $(this).data('class');
        $('#leave_id').val(leaveId);
        $('#student_id').val(studentId);
        $('#old_school_id').val(schoolId);
        $('#old_class_id').val(classId);

        layer.open({
            type: 1,
            btn: ['提交'],
            title: '调课',
            content: $('#adjust_course_schedule'),
            yes: function (index, layero) {
                loading = layer.load(0, { shade: false });
                adjust_student_schedule();
            }
        });
    });

    //请求调课
    function adjust_student_schedule()
    {
        var adjust_school_id = $('#adjust_school_id').val();
        var adjust_class_id = $('#adjust_class_id').val();
        var adjust_course_time = $('#adjust_course_time').val();
        if(!adjust_school_id || !adjust_class_id || !adjust_course_time){
            return false;
        }

        var adjust_data = $('#form_adjust').serialize();
        $.ajax({
            url:"{:url('Classes/adjustSchedule')}",
            type:'post',
            dataType:'json',
            data: adjust_data,
            success:function(jsonData){
                layer.msg(jsonData.Message, { icon: jsonData.Status, time: 1500 }, function () {
                    layer.closeAll();
                    window.location.reload();
                });
            },
            error:function(XMLHttpRequest, textStatus, errorThrown){
                console.log('xhr='+XMLHttpRequest.readyState+' status='+textStatus+' error='+errorThrown);
            }
        });
    }

    //根据学校选择班级(调课)
    $('#adjust_school_id').on('change',function () {
        var sid = $(this).val();
        if(sid == 0){
            $('#adjust_class_id').empty();
            var html = '<option value=\"0\">--- 请选择 ---</option>';
            $('#adjust_class_id').html(html);
            return false;
        }
        $.ajax({
            url:"{:url('Classes/ajax_class')}",
            type:'post',
            dataType:'json',
            data:{"school_id":sid},
            success:function(jsonData){
                $('#adjust_class_id').empty();
                var html = '<option value=\"0\">--- 请选择 ---</option>';

                if(jsonData.code == 1){
                    for(var i in jsonData.data){
                        html += '<option value=\"'+jsonData.data[i].id+'\">'+jsonData.data[i].name+'</option>';
                    }
                }
                $('#adjust_class_id').html(html);
            },
            error:function(XMLHttpRequest, textStatus, errorThrown){
                console.log('xhr='+XMLHttpRequest.readyState+' status='+textStatus+' error='+errorThrown);
            }
        });
    });

    //根据班级查找排课
    $('#adjust_class_id').on('change',function () {
        var cid = $(this).val();
        if(cid == 0){
            $('#adjust_course_time').empty();
            var html = '<option value=\"0\">--- 请选择 ---</option>';
            $('#adjust_course_time').html(html);
            return false;
        }
        $.ajax({
            url:"{:url('Classes/ajax_schedule')}",
            type:'post',
            dataType:'json',
            data:{"class_id":cid},
            success:function(jsonData){
                $('#adjust_course_time').empty();
                var html = '<option value=\"0\">--- 请选择 ---</option>';

                if(jsonData.code == 1){
                    var schedule = jsonData.data;
                    for(var i in schedule){
//                        html += '<option value=\"'+schedule[i].date+','+schedule[i].start_time+','+schedule[i].end_time+','+schedule[i].week+','+schedule[i].schedule+'\">'+
                        html += '<option value=\"'+schedule[i].id+','+schedule[i].schedule+'\">'+
                            schedule[i].date_time+' '+schedule[i].start_time+'-'+schedule[i].end_time+' '+schedule[i].week+' '+schedule[i].schedule+'</option>';
                    }
                }
                $('#adjust_course_time').html(html);
            },
            error:function(XMLHttpRequest, textStatus, errorThrown){
                console.log('xhr='+XMLHttpRequest.readyState+' status='+textStatus+' error='+errorThrown);
            }
        });
    });

    /**************查看调课列表****************/
    $(document).on('click','.adjust_detail',function () {
        var studentId = $(this).data('student');
        var schoolId = $(this).data('school');
        var classId = $(this).data('class');

        $.ajax({
            url:"{:url('Classes/ajax_adjust_list')}",
            type:'post',
            dataType:'json',
            data:{"student_id":studentId, "school_id":schoolId, "class_id":classId},
            success:function(jsonData){
                console.log(jsonData);
                $('#table_adjust').empty();
                var init_html = '<thead><tr>' +
                    '<th>请假编号</th><th>学生</th><th>学校</th><th>班级</th><th>课时</th><th>星期</th><th>进度</th><th>操作</th>' +
                    '</tr></thead>';
                var html = '';
                if(jsonData.code == 1){
                    var adjust = jsonData.data;
                    for(var i in adjust){
                        html += '<tr>';
                        html += '<td>'+adjust[i].leave_id+'</td><td>'+adjust[i].student_name+'</td><td>'+adjust[i].school_name+'</td><td>'+adjust[i].class_name+'</td>' +
                            '<td>'+adjust[i].date+' '+adjust[i].start_time+'-'+adjust[i].end_time+'</td><td>'+adjust[i].week+'</td><td>'+adjust[i].schedule+'/'+adjust[i].course_hour+'</td>' +
                            '<td><a href=\"'+adjust[i].delete_url+'\" data-msg=\"确定要删除调课吗？\" class=\"js-ajax-delete\">删除</a></td>';
                        html += '</tr>';
                    }
                }
                var last_html = init_html + html;
                $('#table_adjust').html(last_html);

                layer.open({
                    type: 1,
//                    btn: ['提交'],
                    title: '调课列表',
                    area:['auto','auto'],
                    minwidth:'360px',
                    content: $('#adjust_schedule_detail'),
                    yes: function (index, layero) {
                        loading = layer.load(0, { shade: false });
                        adjust_student_schedule();
                    }
                });
            },
            error:function(XMLHttpRequest, textStatus, errorThrown){
                console.log('xhr='+XMLHttpRequest.readyState+' status='+textStatus+' error='+errorThrown);
            }
        });


    });

</script>
</body>